<?php

/**
 * This is the model class for table "company".
 *
 * The followings are the available columns in table 'company':
 * @property string $id
 * @property string $company_name
 * @property string $short_description
 * @property string $description
 * @property string $website
 * @property string $person_name
 * @property string $person_phone
 * @property string $person_email
 * @property string $address
 */
class Company extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Company the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'company';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('company_name, user_id, short_description, description, person_name, person_email', 'required'),
			array('company_name, website, person_name, person_email', 'length', 'max'=>255),
			array('person_phone', 'length', 'max'=>45),
			array('website', 'url', 'pattern'=>'/^(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i'),
			array('person_email', 'email'),
			array('short_description, description, address, is_active', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, company_name, short_description, description, website, person_name, person_phone, person_email, address', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'company_name' => 'Company Name',
			'short_description' => 'Short Description',
			'description' => 'Description',
			'website' => 'Website',
			'person_name' => 'Person Name',
			'person_phone' => 'Person Phone',
			'person_email' => 'Person Email',
			'address' => 'Address',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('company_name',$this->company_name,true);
		$criteria->compare('short_description',$this->short_description,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('website',$this->website,true);
		$criteria->compare('person_name',$this->person_name,true);
		$criteria->compare('person_phone',$this->person_phone,true);
		$criteria->compare('person_email',$this->person_email,true);
		$criteria->compare('address',$this->address,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 
	 */
	public function getActiveCompanies() {
		$criteria = new CDbCriteria;
		$criteria->condition = 'is_active = 1';
		$criteria->order = '`company_name` ASC, `id` ASC';
		
		return Company::model()->findAll($criteria);		
	}
	
	/**
	 * Get companys by user id
	 * @param unknown_type $id
	 */
	public static function getByUserId($id) {
		$criteria = new CDbCriteria;
		$criteria->condition = 'is_active = 1 AND user_id=:userId';
		$criteria->params = array(':userId'=>$id);
		$criteria->order = '`company_name` ASC, `id` ASC';
		
		return Company::model()->findAll($criteria);
	}
}